;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.StockAllocationPlan", [])
        .controller("StockAllocationPlanCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter, $stateParams) {
            $scope.activeFlag = [true, false, false, false, false];
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };

            $scope.tabFlag = {};
            $scope.orderObj = {
                detailVOList: []
            };

            $scope.exportParam = {
                exportUrl: ybHttp.RequestUrl("device", "/regionAllotPlan/exportAll"),
                sectionParam: {
                    idList: []
                }
            };
            $scope.condition = {};
            $scope.datePickerFlag = [false, false];
            $scope.statusList = [
                {
                    code: 0,
                    name: '编辑'
                },
                {
                    code: 1,
                    name: '已提交'
                },
                {
                    code: 2,
                    name: '部分调拨'
                },
                {
                    code: 3,
                    name: '结单'
                },
                {
                    code: 4,
                    name: '已完成'
                },
            ];
            $scope.statusListForCankao = [
                {
                    code: 1,
                    name: '已提交'
                },
                {
                    code: 2,
                    name: '部分调拨'
                },
            ];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.dataList = [];

            $scope.markInfo = {
                allCheckedBill: false,
                allCheckedMaterial: false,
                curRow: -1
            };

            var materielFieldListForAdd = [
                {
                    name: "零件编号",
                    width: 150,
                    dataField: "materialsNo",
                    needInput: false
                },
                {
                    name: "零件中文名",
                    width: 200,
                    dataField: "materialsDes",
                    needInput: false
                },
                {
                    name: "单位",
                    width: 100,
                    dataField: "unit",
                    needInput: false
                },
                {
                    name: "实际库存",
                    width: 100,
                    dataField: "enableNum",
                    clickFn: "alterReport",
                    needInput: false
                },
                {
                    name: "意向占用",
                    width: 100,
                    dataField: "intentionNum",
                    needInput: false
                },
                {
                    name: "发货占用",
                    width: 100,
                    dataField: "deliveryNum",
                    needInput: false
                },
                {
                    name: "计划调拨数量",
                    width: 120,
                    dataField: "planAmount",
                    needInput: true,
                    needRequired: true
                },
                {
                    name: "备注",
                    width: 100,
                    dataField: "remark",
                    needInput: true,
                    needRequired: false
                }
            ];

            var materielFieldList = [
                {
                    name: "零件编号",
                    width: 150,
                    dataField: "materialsNo",
                    needInput: false
                },
                {
                    name: "零件中文名",
                    width: 200,
                    dataField: "materialsDes",
                    needInput: false
                },
                {
                    name: "单位",
                    width: 100,
                    dataField: "unit",
                    needInput: false
                },
                {
                    name: "实际库存",
                    width: 100,
                    dataField: "enableNum",
                    clickFn: "alterReport",
                    needInput: false
                },
                {
                    name: "意向占用",
                    width: 100,
                    dataField: "intentionNum",
                    needInput: false
                },
                {
                    name: "发货占用",
                    width: 100,
                    dataField: "deliveryNum",
                    needInput: false
                },
                {
                    name: "计划调拨数量",
                    width: 120,
                    dataField: "planAmount",
                    needInput: true,
                    needRequired: true
                },
                {
                    name: "已创建区域调拨单数量",
                    width: 150,
                    dataField: "allotAmount",
                    needInput: false
                },
                {
                    name: "单据状态",
                    width: 100,
                    dataField: "planStatusShow",
                    needInput: false
                },
                {
                    name: "备注",
                    width: 100,
                    dataField: "remark",
                    needInput: true,
                    needRequired: false
                }
            ];

            // 新增 表格列配置
            $scope.materielAddConfig = {
                scrollWidth: 970,
                tableCol: materielFieldListForAdd
            };

            // 编辑 表格列配置
            $scope.materielTableConfig = {
                scrollWidth: 1220,
                tableCol: materielFieldList
            };

            // 详情 表格列配置
            $scope.materielDetailConfig = {
                scrollWidth: 1220,
                tableCol: materielFieldList
            };

            // 首页列表 表格列配置
            $scope.tableConfig = {
                fixedWidth: 180,
                scrollWidth: 1660,
                tableCol: [
                    {
                        type: "",
                        name: "调拨计划单号",
                        dataField: "allotPlanOrder",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单部门",
                        dataField: "dept",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单时间",
                        dataField: "createDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调出仓库",
                        dataField: "outWarehouse",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调出仓库描述",
                        dataField: "outWarehouseDesc",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调入仓库",
                        dataField: "inWarehouse",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调入仓库描述",
                        dataField: "inWarehouseDesc",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "收货人",
                        dataField: "receiveUser",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "收货人联系方式",
                        dataField: "receiveTel",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "收货地址",
                        dataField: "receiveAddress",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "移动类型",
                        dataField: "moveType",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单据状态",
                        dataField: "planStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "CRM单号",
                        dataField: "crmOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 140,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.search = function (curpage) {
                $scope.condition.planStatusList = []; // 清空参考创建区域调拨计划的单据状态查询条件
                getDataList(curpage);
            };

            $scope.searchForCanKao = function (curpage) {
                if ($scope.condition.planStatus) {
                    $scope.condition.planStatusList = null;
                } else {
                    $scope.condition.planStatusList = [1, 2];
                }
                getDataList(curpage);
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        $scope.search();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        $scope.searchForCanKao();
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.orderObj = {
                    detailVOList: []
                };
                $scope.markInfo.allCheckedMaterial = false;
                $scope.toolbarConfig.showMateriel = false;
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    allCheckedMaterial: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.idList = [];
                var service = "device";
                var reqUrl = "/regionAllotPlan/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateShow = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                        item.planStatusShow = $filter("ProgressStatus")(item.planStatus, "allotPlanStatus");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    return resp.value.list;
                });
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function getBillIds() {
                $scope.exportParam.sectionParam.idList = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.idList.push(item.id);
                    }
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    case "alterReport":
                        alterReport(item);
                        break;
                    default:
                        break;
                }
            };

            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.orderObj = data;
                    $scope.changeTab(1);
                });
            }

            function alterReport(item) {
                var param = {
                    materialsNo: item.materialsNo
                };
                ybWidget.StorageReport(param);
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDatePicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.addApply = function () {
                $scope.tabFlag.type = 1;
                $scope.orderObj.createDate = +new Date();
                $scope.orderObj.moveType = '311';
                $scope.orderObj.deptId = $scope.curUser.deptId;
                $scope.orderObj.dept = $scope.curUser.deptName;
                $scope.orderObj.createName = $scope.curUser.name;
                $scope.changeTab(2);
            };

            $scope.searchDispatch = function () {
                var param = {
                    name: "区域调拨调度员",
                    defaultRoleName: '',
                    type: 'staffByWorkspace',
                    supplierOrgId: $scope.curOrgId
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.orderObj.dispatcherName = data.name;
                    $scope.orderObj.dispatcherId = data.userId;
                })
            }

            $scope.selectWarehouse = function (type, selectedId) {
                ybWidget.WarehouseSelector({
                    needPermit: true,
                    selectedId: selectedId
                }).then(function (data) {
                    if (type == 'in') {
                        if (!!$scope.orderObj.inWarehouse && (data.warehouseCode !== $scope.orderObj.inWarehouse)) {}
                        $scope.orderObj.inWarehouse = data.warehouseCode;
                    }
                    if (type == 'out') {
                        $scope.orderObj.outWarehouse = data.warehouseCode;
                        findDeliveryNum()
                    }
                })
            };

            $scope.clearWarehouse = function (type) {
                if (type == 'in') {
                    $scope.orderObj.inWarehouse = "";
                }
                if (type == 'out') {
                    $scope.orderObj.outWarehouse = "";
                    findDeliveryNum();
                }
            };

            // 实时更新库存数量
            function findDeliveryNum() {
              if ($scope.orderObj.detailVOList.length <= 0) {
                return;
              }

              var service = "device";
              var reqUrl = "/materials/findDeliveryNum";
              var param = {};
              param.warehouseCode = $scope.orderObj.outWarehouse;
              param.materialsVOS = $scope.orderObj.detailVOList.map(item => ({materialsNo: item.materialsNo}))
              return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                  console.log(resp.value);
                  if (resp.code !== 0) {
                      ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                      return false;
                  }
                  if (!resp.value) {
                      return;
                  }
                  angular.forEach($scope.orderObj.detailVOList, function (item) {
                      angular.forEach(resp.value.materialsVOS, function (itemDetail) {
                          if (item.materialsNo === itemDetail.materialsNo) {
                              item.deliveryNum = itemDetail.deliveryNum;
                              item.enableNum = itemDetail.enableNum;
                            }
                      });
                  });
              }).catch(err => {
                console.log(err);
              });
            }

            $scope.editApply = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.tabFlag.type = 2;
                    $scope.orderObj = data;
                    $scope.changeTab(3);
                });
            };

            $scope.adjustApply = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.orderObj = data;
                    $scope.changeTab(5);
                });
            };

            $scope.delApply = function (id) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    var service = "device";
                    var reqUrl = "/regionAllotPlan/delete";
                    ybHttp.Post(service, reqUrl, [id]).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.search();
                    });
                })
            };

            $scope.finishByIds = function (id) {
                ybWidget.ShowMessage("确定要结单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/regionAllotPlan/manuFinish";
                    ybHttp.Post(service, reqUrl, [id]).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.search();
                    });
                }, function () {});
            }

            $scope.finishByDetailIds = function (id) {
                ybWidget.ShowMessage("确定要结单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/regionAllotPlan/manuFinishByDetailIds";
                    ybHttp.Post(service, reqUrl, [id]).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.changeTab(0);
                    });
                }, function () {});
            }

            function getDetail(item) {
                var param = {
                    id: item.id
                };
                return ybHttp.Get('device', '/regionAllotPlan/item', param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    angular.forEach(resp.value.detailVOList, function (item) {
                        item.materialsDes = item.materialName;
                        item.materialsNo = item.materialNo;
                        item.planStatusShow = $filter("ProgressStatus")(item.planStatus, "allotPlanStatus");
                        checkItemStockStatus(item, item.planAmount);
                    });
                    return resp.value;
                });
            }

            $scope.selMateriel = function (e) {
                if (!!$scope.toolbarConfig.showMateriel) {
                    e.stopPropagation();
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig.showMateriel = true;
                        EventBus.Publish("getMaterial");
                    });
                }
            };

            $scope.importMateriel = function () {
                var param = {
                    name: "零件",
                    templateReqService: "device",
                    templateReqUrl: "/regionAllot/download",
                    importReqService: "device",
                    importReqUrl: "/regionAllot/import",
                    importErrorUrl: "",
                    warehouseCode: $scope.orderObj.outWarehouse
                };
                ybWidget.ImportFile(param).then(function (data) {
                    angular.forEach(data, function (item) {
                        item.isChecked = false;
                        item.materialsDes = item.materialName;
                        item.materialsNo = item.materialNo;
                        item.planAmount = item.amount;
                    });
                    $scope.orderObj.detailVOList = [];
                    $scope.orderObj.detailVOList = angular.copy(data);
                });
            };

            $scope.batchDelMateriel = function () {
                var selectedCount = 0;
                for (var i = 0, len = $scope.orderObj.detailVOList.length; i < len; i++) {
                    if (!!$scope.orderObj.detailVOList[i].isChecked) {
                        selectedCount++;
                    }
                }
                if (!selectedCount) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除选中的记录吗？", MESG_TYPE.Ask).then(function () {
                    for (var i = $scope.orderObj.detailVOList.length - 1; i >= 0; i--) {
                        if (!!$scope.orderObj.detailVOList[i].isChecked) {
                            if ((!!$scope.orderObj.detailVOList[i].inState && $scope.orderObj.detailVOList[i].inState !== 0) || (!!$scope.orderObj.detailVOList[i].outState && $scope.orderObj.detailVOList[i].outState !== 0)) {
                                ybWidget.ShowMessage("只能删除未调出的零件", MESG_TYPE.Warn);
                                return;
                            }
                            $scope.orderObj.detailVOList.splice(i, 1);
                        }
                    }
                    if ($scope.orderObj.detailVOList.length === 0) {
                        $scope.markInfo.allCheckedMaterial = false;
                    }
                    EventBus.Publish("setMaterial");
                });
            };

            $scope.delMateriel = function (item, index) {
                ybWidget.ShowMessage("确定要删除该记录吗？", MESG_TYPE.Ask).then(function () {
                    $scope.orderObj.detailVOList.splice(index, 1);
                    EventBus.Publish("setMaterial", item.materialsNo);
                });
            };

            $scope.showQtyTip = function (item) {
                if (!!item.qtyStockErr) {
                    item.showStockErr = true;
                }
            };

            $scope.hideQtyTip = function (item) {
                item.showStockErr = false;
            };

            $scope.checkQty = function (item) {
                item.qtyErr = false;
                if (!item.planAmount || parseFloat(item.planAmount) <= 0) {
                    item.qtyErr = true;
                    return;
                }
                checkItemStockStatus(item, item.planAmount);
            };

            function checkItemStockStatus(item, currentQty) {
                UserContextService.SetStockStatusFlag(item, currentQty);
            }

            $scope.saveData = function (type, invalid) {
                if (!invalid) {
                    $scope.submitted = true;
                    return;
                }
                if ($scope.orderObj.detailVOList.length === 0) {
                    ybWidget.ShowMessage("零件不可为空", MESG_TYPE.Error);
                    return;
                }
                if (type!='adjust' && isErrorStockStatusForExcavatorCompany()) {
                  return;
                }

                var service = "device";
                var reqUrl = "/regionAllotPlan/" + type;

                var listValid = true;
                for (var i = 0, len = $scope.orderObj.detailVOList.length; i < len; i++) {
                    $scope.orderObj.detailVOList[i].materialName = $scope.orderObj.detailVOList[i].materialsDes;
                    $scope.orderObj.detailVOList[i].materialNo = $scope.orderObj.detailVOList[i].materialsNo;
                    if (!$scope.orderObj.detailVOList[i].planAmount || parseFloat($scope.orderObj.detailVOList[i].planAmount) <= 0) {
                        listValid = false;
                        $scope.orderObj.detailVOList[i].qtyErr = true;
                        break;
                    }
                }
                if (!listValid) {
                    return;
                }

                var param = angular.copy($scope.orderObj);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                });
            };

            function isErrorStockStatusForExcavatorCompany() {
                if ($scope.isWJ && ['WJBJ', '9200', '9300'].includes($scope.orderObj.outWarehouse)) {
                    return false
                }

                if (!($scope.isWJ || $scope.isKJ)) {
                    return false;
                }

                for (var i = 0; i < $scope.orderObj.detailVOList.length; i++) {
                    if ($scope.orderObj.detailVOList[i].outState === undefined || $scope.orderObj.detailVOList[i].outState === 0) {
                        checkItemStockStatus($scope.orderObj.detailVOList[i], $scope.orderObj.detailVOList[i].planAmount);
                        if ($scope.orderObj.detailVOList[i].qtyStockErr && ($scope.orderObj.detailVOList[i].qtyStockErrLevel > 1)) {
                            ybWidget.ShowMessage("零件" + $scope.orderObj.detailVOList[i].materialsNo + "(" + $scope.orderObj.detailVOList[i].materialsDes + ")库存不足，单据不能创建，请先删除该零件或取消提交", MESG_TYPE.Error);
                            $scope.clickValue = false;
                            return true;
                        }
                    }
                }
                return false;
            }

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.makeAllotOrder = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    data.createdStorage = true;
                    $state.go("app.stock.allocation", {
                        canKaoObj: data
                    })
                });
            }


            var btnPermissionCode = {
                stockAllocationPlanAdd: window.ActionCode.stockAllocationPlanAdd,
                stockAllocationPlanSubmit: window.ActionCode.stockAllocationPlanSubmit,
                stockAllocationPlanDel: window.ActionCode.stockAllocationPlanDel,
                stockAllocationPlanExport: window.ActionCode.stockAllocationPlanExport,
                stockAllocationPlanFinish: window.ActionCode.stockAllocationPlanFinish
            };

            $scope.btnPermission = {
                stockAllocationPlanAdd: true,
                stockAllocationPlanSubmit: true,
                stockAllocationPlanDel: true,
                stockAllocationPlanExport: true,
                stockAllocationPlanFinish: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                $scope.setFixedWidth();
                if ($stateParams.isCanKao) {
                    $scope.changeTab(4);
                } else {
                    $scope.changeTab(0);
                }
            })();
        }])

    ;
})(window, window.angular);